package com.heh.mybatisplus.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @项目名： spring-emq
 * @包名： com.heh.mybatisplus.entity
 * @文件名: BaseEntity
 * @创建者: liufudong@szyh-smart.com
 * @创建时间: 2024/10/23 22:54
 * @描述：
 */

@JsonInclude(JsonInclude.Include.NON_NULL)
public class BaseEntity {
    @TableField(
            exist = false
    )
    private int pageNo = 1;
    @TableField(
            exist = false
    )
    private int pageSize = 20;
    @TableField(
            exist = false
    )
    private List<String> orderList;
    @TableField(
            exist = false
    )
    private Integer dataLimit;
    @TableField(
            exist = false
    )
    private Boolean countQuery = false;

    public Integer getStart() {
        return this.pageNo >= 0 && this.pageSize >= 0 ? (this.pageNo - 1) * this.pageSize : 0;
    }

    public BaseEntity pushOrder(String order) {
        if (StringUtils.isBlank(order)) {
            return this;
        } else {
            order = order.trim();
            if (!order.contains(" asc") && !order.contains(" desc")) {
                return this;
            } else {
                if (null == this.orderList) {
                    this.orderList = new ArrayList();
                }

                this.orderList.add(order);
                return this;
            }
        }
    }

    @JsonIgnore
    @JSONField(
            serialize = false
    )
    public String getOrders() {
        return CollectionUtils.isNotEmpty(this.orderList) ? (String)this.orderList.stream().collect(Collectors.joining(",")) : "";
    }

    public String toString() {
        try {
            return (new ObjectMapper()).writeValueAsString(this);
        } catch (JsonProcessingException var2) {
            return JSON.toJSONString(this);
        }
    }

    @JsonIgnore
    @JSONField(
            serialize = false
    )
    public String getJson() {
        return JSONObject.toJSONString(this, new SerializerFeature[]{SerializerFeature.WriteDateUseDateFormat});
    }

    public BaseEntity() {
    }

    public int getPageNo() {
        return this.pageNo;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public List<String> getOrderList() {
        return this.orderList;
    }

    public Integer getDataLimit() {
        return this.dataLimit;
    }

    public Boolean getCountQuery() {
        return this.countQuery;
    }

    public BaseEntity setPageNo(int pageNo) {
        this.pageNo = pageNo;
        return this;
    }

    public BaseEntity setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public BaseEntity setOrderList(List<String> orderList) {
        this.orderList = orderList;
        return this;
    }

    public BaseEntity setDataLimit(Integer dataLimit) {
        this.dataLimit = dataLimit;
        return this;
    }

    public BaseEntity setCountQuery(Boolean countQuery) {
        this.countQuery = countQuery;
        return this;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof BaseEntity)) {
            return false;
        } else {
            BaseEntity other = (BaseEntity)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.getPageNo() != other.getPageNo()) {
                return false;
            } else if (this.getPageSize() != other.getPageSize()) {
                return false;
            } else {
                label52: {
                    Object this$orderList = this.getOrderList();
                    Object other$orderList = other.getOrderList();
                    if (this$orderList == null) {
                        if (other$orderList == null) {
                            break label52;
                        }
                    } else if (this$orderList.equals(other$orderList)) {
                        break label52;
                    }

                    return false;
                }

                Object this$dataLimit = this.getDataLimit();
                Object other$dataLimit = other.getDataLimit();
                if (this$dataLimit == null) {
                    if (other$dataLimit != null) {
                        return false;
                    }
                } else if (!this$dataLimit.equals(other$dataLimit)) {
                    return false;
                }

                Object this$countQuery = this.getCountQuery();
                Object other$countQuery = other.getCountQuery();
                if (this$countQuery == null) {
                    if (other$countQuery != null) {
                        return false;
                    }
                } else if (!this$countQuery.equals(other$countQuery)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof BaseEntity;
    }

    public int hashCode() {
//        int PRIME = true;
        int result = 1;
        result = result * 59 + this.getPageNo();
        result = result * 59 + this.getPageSize();
        Object $orderList = this.getOrderList();
        result = result * 59 + ($orderList == null ? 43 : $orderList.hashCode());
        Object $dataLimit = this.getDataLimit();
        result = result * 59 + ($dataLimit == null ? 43 : $dataLimit.hashCode());
        Object $countQuery = this.getCountQuery();
        result = result * 59 + ($countQuery == null ? 43 : $countQuery.hashCode());
        return result;
    }
}
